﻿using KPIS.GERP.Models.Class;
using System;
using System.Linq;
using System.Web.Mvc;
using Resources;
using System.Collections.Generic;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    public class MasPurchaseMethodController : Controller
    {
        //
        // GET: /MasPurchaseMethod/

        public ActionResult Index()
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseMethod(new Models.ICM.MasPurchaseMethod { RecordStatus = "U" }, new[] { "MasPurchaseType" });
            return View(model);
        }

        public ActionResult Details(int id)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseMethod(new Models.ICM.MasPurchaseMethod { PcMethodId = id }, new[] { "MasPurchaseType" }).FirstOrDefault();
            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Models.ICM.MasPurchaseMethod model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();
                    var id = service.InsertPurchaseMethod(model, Class.GimsMembership.CurrentEmpId);
                    return RedirectToAction("Details", new { id });
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return View(model);
        }


        public ActionResult Edit(int id)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectPurchaseMethod(new Models.ICM.MasPurchaseMethod { PcMethodId = id }, new[] { "MasPurchaseType" }).FirstOrDefault();
            return View(model);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Edit(int id, Models.ICM.MasPurchaseMethod model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new IcmMasterService.IcmMasterServiceClient();
                    service.UpdatePurchaseMethod(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new { id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new IcmMasterService.IcmMasterServiceClient();
                service.DeletePurchaseMethod(id, Class.GimsMembership.CurrentEmpId);
                service.Close();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            if (deleteIds == null)
                return Json(new { success = true });

            try
            {
                var service = new IcmMasterService.IcmMasterServiceClient();
                foreach (int delete in deleteIds)
                {
                    service.DeletePurchaseMethod(delete, Class.GimsMembership.CurrentEmpId);
                }

                ModelState.AddModelError(ErrorLocalization.Error, string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
                GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
                return Json(new { success = false });
            }
        }

        public List<SelectListItem> Drobdown()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "-- ทั้งหมด --", Value = "0", Selected = true });
            items.Add(new SelectListItem { Text = "หลักประกันสัญญา", Value = "29" });
            items.Add(new SelectListItem { Text = "หลักประกันซอง", Value = "30" });
            items.Add(new SelectListItem { Text = "หลักประกันผลงาน", Value = "73" });
            return items;
        }
    }
}
